package com.rural.bus.driver;

import org.json.JSONException;
import org.json.JSONObject;

import com.rural.bus.driver.config.MyPreferenceManager;
import com.rural.bus.driver.config.PreferenceKey;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;
import com.rural.bus.driver.utils.CommonTools;
import com.rural.bus.driver.utils.ProgressUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class FeedbackActivity extends BaseActivity {

	private EditText etFeedback;
	private String userId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);

		initView();
		setListeners();
		initData();
	}

	@Override
	protected void initView() {

		etFeedback = (EditText) findViewById(R.id.et_feedback);
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("投诉建议");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));

		mBtRight.setText("确定");
		mBtRight.setVisibility(View.VISIBLE);
		mBtRight.setTextColor(getResources().getColor(R.color.white));

		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void setListeners() {

		mImgLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		mBtRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				submitFeedback();
			}
		});
	}

	private void submitFeedback() {
		userId = MyApplication.driverInfo.drivId;

		if (CommonTools.isEmpty(userId)) {
			toast("请先登录");
			return;
		}

		String message = etFeedback.getText().toString().trim();
		if (CommonTools.isEmpty(message)) {
			toast("意见反馈不能为空");
			return;
		}

		MyHttpUtil http = new MyHttpUtil();
		http.addBodyParm("authorId", "" + userId);
		http.addBodyParm("type", "" + 1); // 0 乘客 1 司机
		http.addBodyParm("remarks", "" + message);
		http.addBodyParm("name", "" +  MyApplication.driverInfo.name);
		http.addBodyParm("phpne", "" + MyApplication.driverInfo.phone);

		ProgressUtil.showProgress(this, "");
		http.send(Url.update_url, new OnNetCallback() {

			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				try {
					if (object.get("status").equals("0")) {
						toast("提交成功");
						FeedbackActivity.this.finish();
					} else {
						toast(R.string.data_error);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
			}
		});
	}

}
